<?php header("content-Type: text/html; charset=UTF-8");
date_default_timezone_set("PRC");
?>
<?php
	//修改php.ini，将error_reporting = E_ALL 修改为error_reporting = E_ALL & ~E_NOTICE
	//包含数据库连接文件  
	include('../api/conn.php');

	//api class file
	include('../api/PaymentClient.class.php');


//判断是否为移动端
function isMobile() { 
  // 如果有HTTP_X_WAP_PROFILE则一定是移动设备
  if (isset($_SERVER['HTTP_X_WAP_PROFILE'])) {
    return true;
  } 
  // 如果via信息含有wap则一定是移动设备,部分服务商会屏蔽该信息
  if (isset($_SERVER['HTTP_VIA'])) { 
    // 找不到为flase,否则为true
    return stristr($_SERVER['HTTP_VIA'], "wap") ? true : false;
  } 
  // 脑残法，判断手机发送的客户端标志,兼容性有待提高。其中'MicroMessenger'是电脑微信
  if (isset($_SERVER['HTTP_USER_AGENT'])) {
    $clientkeywords = array('nokia','sony','ericsson','mot','samsung','htc','sgh','lg','sharp','sie-','philips','panasonic','alcatel','lenovo','iphone','ipod','blackberry','meizu','android','netfront','symbian','ucweb','windowsce','palm','operamini','operamobi','openwave','nexusone','cldc','midp','wap','mobile','MicroMessenger'); 
    // 从HTTP_USER_AGENT中查找手机浏览器的关键字
    if (preg_match("/(" . implode('|', $clientkeywords) . ")/i", strtolower($_SERVER['HTTP_USER_AGENT']))) {
      return true;
    } 
  } 
  // 协议法，因为有可能不准确，放到最后判断
  if (isset ($_SERVER['HTTP_ACCEPT'])) { 
    // 如果只支持wml并且不支持html那一定是移动设备
    // 如果支持wml和html但是wml在html之前则是移动设备
    if ((strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') !== false) && (strpos($_SERVER['HTTP_ACCEPT'], 'text/html') === false || (strpos($_SERVER['HTTP_ACCEPT'], 'vnd.wap.wml') < strpos($_SERVER['HTTP_ACCEPT'], 'text/html')))) {
      return true;
    } 
  } 
  return false;
}
	
//	var_dump($_POST);
	if(!isset($_POST['payType']))
			exit('参数错误！');
	$payType				= $_POST['payType'];
//	$payBank				= $_POST['payBank'];
	$userid 				= $_POST['userid'];							//user_id
	$bankCode				= $_POST['bankkey'];						//bank code
	if(isset($_POST['acc_type']))
		$accType				= $_POST['acc_type'];
	else
		$accType				="2";
		
	$order_amount 	= $_POST['amount'];			//???
	$order_time 		= date("Y-m-d H:i:s",time());
	$deposit_remark = iconv("utf-8","gb2312//IGNORE", "在线订单");			//??¡�?
	$order_no 			= date('YmdHis') . mt_rand(10000, 99999);
	$attach					= "online";

//	$userid=47619;	
	if(strlen($userid) < 2 || strlen($userid) > 12)
		exit("用户ID 不正确");
		

	// check the userid
	$sql="select * from t_user where t_id=$userid";
	$query=sqlsrv_query($conn,$sql);
	$result=sqlsrv_fetch_array($query,SQLSRV_FETCH_NUMERIC);
	if(empty($result)){
		//echo "用户ID 不存在";
		exit("用户ID 不存在");
	}
	//check the ordernumber
	$sql="select * from t_chongzhi_log where order_no='$order_no' and t_user_id=$userid";
	
	$query=sqlsrv_query($conn,$sql);
	$result=sqlsrv_fetch_array($query,SQLSRV_FETCH_NUMERIC);
	if(empty($result)){
		$content = iconv("utf-8","gb2312//IGNORE","在线充值");
		$sql="insert into t_chongzhi_log (t_user_id,t_jine,t_time,t_state,t_pingtai,t_tishi,order_no,attach,comments) 
				values ($userid,$order_amount,'$order_time',0, '$content',0, '$order_no', '$attach','$deposit_remark')";
	//			echo $sql;
		$query=sqlsrv_query($conn,$sql);		
		//print_r(sqlsrv_errors());
		//exit;
	}
	else
		echo 'orderno exists';
		
    
    
//根据定义的payType进行修改即可
			if($payType == "0" || $payType == "")
			{
				$payType="WY";							//WYKJ
				$merchantCode	= $merchantCodeXH;
				$merchantKey 	= $merchantKeyXH;
				$apiBaseUri		= $apiBaseUriXH;
			}
			else
			{
				//obj1:1=微信；2=支付宝；3=QQ；4=银联；5=京东;6=微信H5，7=支付宝H5
				if($payType == "1")
				{
					if(isMobile())
						$payType="WXH5";
					else
						$payType = "WXQR";				//WXQR//WXTM//WXH5				
				}
				else if($payType == "2")
				{
				//	if(isMobile())
				//		$payType="ALIH5";
				//	else
						$payType = "ALIH5";				//ALIQR//ALITM//ALIH5					
				}
				else if($payType == "3")
				{
					if(isMobile())
							$payType = "QQH5";				//QQQR//QQTM//QQH5
					else
							$payType = "QQQR";
				}
				else if($payType == "4")
					$payType = "UNQR";				//UNH5//UNKJ
				else if($payType == "5")
					$payType = "JDH5";				//JDH5
				else if($payType == "6")		//微信H5
				{
					if(isMobile())
						$payType="WXH5";
					else
						$payType = "WXQR";				//WXQR//WXTM//WXH5				
				}
				else if($payType == "7")		//支付宝H5
				{
					if(isMobile())
						$payType="ALIH5";
					else
						$payType = "ALIQR";				//ALIQR//ALITM//ALIH5	
				}

				$bankCode = $payType;				//非网银时，bankcode=paytype.
			}
			//商户通知地址
			$returnUrl 	= "http://".$_SERVER['HTTP_HOST']."/return_url.php";	
			$notify_url = "http://".$_SERVER['HTTP_HOST']."/offline_notify.php";	

 			$api = new PaymentClient($merchantCode, $merchantKey, $apiBaseUri);

			//								$orderNo, $payType, $orderAmount, $notifyUrl, $bankCode = '', $returnUrl = '', $returnParams = ''
			if($payType == "WY")
					$order_ret = $api->order_xh($order_no, $payType, $order_amount, $notify_url, $bankCode, $returnUrl, $userid, $accType);
      else
      		$order_ret = $api->order($order_no, $payType, $order_amount, $notify_url, $bankCode, $returnUrl, $userid, $accType);
      
//      var_dump($order_ret);
      
      if($order_ret['is_success'] == 'TRUE')
				header("Location: " .$order_ret['url']);
			else
			{
					echo "<br><br><br>";
					echo "<h3> <font color='red'> 第三方返回消息: <br>".$order_ret['msg'] . " </font> </h3>";
			}
?>